SellerAuthModule = angular.module("SellerAuthModule", [ 'ngRoute' ]);

SellerAuthModule.config(function($routeProvider) {

	$routeProvider.when('/login', {
		templateUrl : 'authentication/login.html',
		controller : 'supLoginCtrl'
	});

	$routeProvider.when('/create-account', {
		templateUrl : 'authentication/create-account.html',
		controller : 'supCreateAccountCtrl'
	});

	$routeProvider.when('/account-created', {
		templateUrl : 'authentication/account-created.html',
		controller : 'emptyCtrl'
	});

	$routeProvider.when('/recover-password', {
		templateUrl : 'authentication/recover-password.html',
		controller : 'supRecoverPasswordCtrl'
	});
	$routeProvider.when('/password-recovered', {
		templateUrl : 'authentication/password-recovered.html',
		controller : 'emptyCtrl'
	});

});

SellerAuthModule.factory('SellerDAO', function($http) {

	return {

		persist : function(seller) {
			return $http.post("/suppliers", seller);
		},
		get : function(id) {
			return $http.get("/suppliers", {
				id : id
			});
		}

	}
});

SellerAuthModule.controller('supLoginCtrl', function($scope, $http, $location) {
	$scope.loginInfo = {
		email : "",
		password : ""
	};

	$scope.signIn = function() {
		$http.post('/suppliers/login', $scope.loginInfo).then(function() {
			console.log("sent");
			
		}, function(data) {
			$scope.errorMessage = data;
		});
	}

});
SellerAuthModule.controller('supCreateAccountCtrl', function($scope, $http,
		$location, SellerDAO) {
	$scope.personForm = true;
	$scope.personInfo = {
		firstName : "",
		lastName : "",
		email : "",
		birthDate : new Date(),
		mobile : "",
		address : "",
		password : "",
		repeatPassword : "",
		terms : false,

	}
	
	

	$scope.signUp = function() {
		
		SellerDAO.persist({
			email : $scope.personInfo.email,
			password : $scope.personInfo.password
		}).then(function(data) {
			$scope.personInfo.id = data;
			$location.path("/account-created");
		}, function(error) {
			$scope.errorMessage = error;
		});
	}
});

SellerAuthModule.controller('SellerAuthCtrl',
		function($scope, $http, $location) {

			window.fbAsyncInit = function() {
				window.fbAsyncInit = undefined;
				FB.init({
					appId : '1393689537528345',
					status : false
				});
				// $scope.login();
			};

			document.querySelector("#fbsdk").setAttribute("src",
					"//connect.facebook.net/en_US/all.js");

		});
SellerAuthModule.controller('supRecoverPasswordCtrl', function($scope, $http,
		$location) {
	$scope.email = "";
	$scope.recoverPassword = function() {
		$http.post("/users/recoverPassword", $scope.email).then(function() {
			$location.path("/password-recovered");
		}, function(info) {
			$scope.errorMessage = info;
		});

	}
});

SellerAuthModule.controller('emptyCtrl', function() {
});

/*
 * FB.getLoginStatus(function(data) { console.log(data); $http({ method :
 * 'POST', url : '/login', data : data }).then(function() { $scope.user = data;
 * });
 * 
 * });
 */